home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / PRED.C < prev    next >
C/C++ Source or Header  |  1992-12-08  |  5KB  |  201 lines

  1. /*
  2.  * pred.c -- source for predicate detection, manipulation
  3.  *
  4.  * SOFTWARE RIGHTS
  5.  *
  6.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  7.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  8.  * company may do whatever they wish with source code distributed with
  9.  * PCCTS or the code generated by PCCTS, including the incorporation of
  10.  * PCCTS, or its output, into commerical software.
  11.  * 
  12.  * We encourage users to develop software with PCCTS.  However, we do ask
  13.  * that credit is given to us for developing PCCTS.  By "credit",
  14.  * we mean that if you incorporate our source code into one of your
  15.  * programs (commercial product, research project, or otherwise) that you
  16.  * acknowledge this fact somewhere in the documentation, research report,
  17.  * etc...  If you like PCCTS and have developed a nice tool with the
  18.  * output, please mention that you developed it using PCCTS.  In
  19.  * addition, we ask that this header remain intact in our source code.
  20.  * As long as these guidelines are kept, we expect to continue enhancing
  21.  * this system and expect to make other tools available as they are
  22.  * completed.
  23.  *
  24.  * ANTLR 1.06
  25.  * Terence Parr
  26.  * Purdue University
  27.  * 1989-1992
  28.  */
  29. #include <stdio.h>
  30. #include "set.h"
  31. #include "syn.h"
  32. #include "hash.h"
  33. #include "generic.h"
  34. #include "dlgdef.h"
  35.  
  36. /* Look for a predicate;
  37.  *
  38.  * Do not pass anything but Junction nodes; no Actions, Tokens, RuleRefs.
  39.  * Return the predicate found if any.  ONLY FINDS ONE FOR NOW.
  40.  */
  41. Predicate
  42. find_predicates(alt)
  43. Node *alt;
  44. {
  45. #ifdef DBG_PRED
  46.     Junction *j;
  47.     RuleRefNode *r;
  48.     TokNode *t;
  49. #endif
  50.     Junction *pr;
  51.     Predicate pred;
  52.     static Predicate pred_empty = {NULL,NULL};
  53.     int max_k=0;
  54.  
  55.     if ( alt==NULL ) return pred_empty;
  56.  
  57. #ifdef DBG_PRED
  58.     switch ( alt->ntype )
  59.     {
  60.         case nJunction :
  61.             j = (Junction *) alt;
  62.             fprintf(stderr, "Junction(in %s)", j->rname);
  63.             switch ( j->jtype )
  64.             {
  65.                 case aSubBlk :
  66.                     fprintf(stderr,"aSubBlk\n");
  67.                     break;
  68.                 case aOptBlk :
  69.                     fprintf(stderr,"aOptBlk\n");
  70.                     break;
  71.                 case aLoopBegin :
  72.                     fprintf(stderr,"aLoopBeginBlk\n");
  73.                     break;
  74.                 case aLoopBlk :
  75.                     fprintf(stderr,"aLoopBlk\n");
  76.                     break;
  77.                 case aPlusBlk :
  78.                     fprintf(stderr,"aPlusBlk\n");
  79.                     break;
  80.                 case EndBlk :
  81.                     fprintf(stderr,"EndBlk\n");
  82.                     break;
  83.                 case RuleBlk :
  84.                     fprintf(stderr,"RuleBlk\n");
  85.                     break;
  86.                 case Generic :
  87.                     fprintf(stderr,"Generic\n");
  88.                     break;
  89.                 case EndRule :
  90.                     fprintf(stderr,"EndRule\n");
  91.                     break;
  92.             }
  93.             break;
  94.         case nRuleRef :
  95.             r = (RuleRefNode *) alt;
  96.             fprintf(stderr, "RuleRef(in %s)\n", r->rname);
  97.             break;
  98.         case nToken :
  99.             t = (TokNode *) alt;
  100.             fprintf(stderr, "TokenNode(in %s)%s\n", t->rname, TokenStr[t->token]);
  101.             break;
  102.         case nAction :
  103.             fprintf(stderr, "Action\n");
  104.             break;
  105.     }
  106. #endif
  107.  
  108.     switch ( alt->ntype )
  109.     {
  110.         case nJunction :
  111.         {
  112.             Predicate a;
  113.             Junction *p = (Junction *) alt;    
  114.             if ( p->jtype == EndRule )
  115.             {
  116.                 return pred_empty;
  117.             }
  118.             if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  119.                  p->jtype==aPlusBlk || p->jtype==EndRule )
  120.             {
  121.                 require(p->pred_lock!=NULL, "rJunc: lock array is NULL");
  122.                 if ( p->pred_lock[1] )
  123.                 {
  124.                     return pred_empty;
  125.                 }
  126.                 p->pred_lock[1] = TRUE;
  127.             }
  128.             a = find_predicates(p->p1);
  129.             if ( a.expr!=NULL )
  130.             {
  131.                 if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  132.                      p->jtype==aPlusBlk || p->jtype==EndRule )
  133.                 {
  134.                     p->pred_lock[1] = FALSE;
  135.                 }
  136.                 return a;
  137.             }
  138.             a = find_predicates(p->p2);
  139.             if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  140.                  p->jtype==aPlusBlk || p->jtype==EndRule )
  141.             {
  142.                 p->pred_lock[1] = FALSE;
  143.             }
  144.             return a;
  145.         }
  146.         case nAction :
  147.         {
  148.             set f;
  149.             ActionNode *p = (ActionNode *) alt;
  150.             if ( p->init_action ) return find_predicates(p->next);
  151.             if ( p->is_predicate )
  152.             {
  153.                 set rk;
  154.                 Tree *t;
  155. #ifdef DBG_PRED
  156.                 fprintf(stderr, "action: <<%s>>?\n", p->action);
  157. #endif
  158.  
  159. #ifdef COMPUTE_CONTEXT
  160.                 ConstrainSearch = 0;
  161.                 TRAV(p->next, LL_k, &rk, t);
  162.                 t = tshrink( t );
  163.                 t = tflatten( t );
  164.                 t = tleft_factor( t );
  165. #ifdef DBG_PRED
  166.                 fprintf(stderr, "after shrink&flatten&left_factor:"); preorder(t); fprintf(stderr, "\n");
  167. #endif
  168. #endif
  169.  
  170.                 pred.expr = p->action;
  171.  
  172. #ifdef COMPUTE_CONTEXT
  173.                 pred.context = t;
  174. #else
  175.                 pred.context = NULL;
  176. #endif
  177.                 return pred;
  178.             }
  179.             return pred_empty;
  180.         }
  181.         case nRuleRef :
  182.         {
  183.             Predicate a;
  184.             RuleRefNode *p = (RuleRefNode *) alt;
  185.             Junction *r;
  186.             int save_halt;
  187.             RuleEntry *q = (RuleEntry *) hash_get(Rname, p->text);
  188.             r = RulePtr[q->rulenum];
  189.             save_halt = r->end->halt;
  190.             r->end->halt = TRUE;
  191.             a = find_predicates((Node *)r->p1);
  192.             r->end->halt = save_halt;
  193.             return a;
  194.         }
  195.         case nToken :
  196.             break;
  197.     }
  198.  
  199.     return pred_empty;
  200. }
  201.